//+------------------------------------------------------------------+
//|                                                  FollowTrade.mqh |
//|                                     Copyright 2014, yellowmouse. |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, yellowmouse."
#property link      ""
//+------------------------------------------------------------------+
//| defines                                                          |
//+------------------------------------------------------------------+
// #define MacrosHello   "Hello, world!"
// #define MacrosYear    2010
//+------------------------------------------------------------------+
//| DLL imports                                                      |
//+------------------------------------------------------------------+
// #import "user32.dll"
//   int      SendMessageA(int hWnd,int Msg,int wParam,int lParam);
// #import "my_expert.dll"
//   int      ExpertRecalculate(int wParam,int lParam);
// #import
//+------------------------------------------------------------------+
//| EX5 imports                                                      |
//+------------------------------------------------------------------+
// #import "stdlib.ex5"
//   string ErrorDescription(int error_code);
// #import
//+------------------------------------------------------------------+
#define Broadcast_Port     54543

#define  SYMBOL_SIZE       32
#define  COMMENT_SIZE      64      
struct MqlTradeRequest_
  {
   ENUM_TRADE_REQUEST_ACTIONS    action;           // Trade operation type
   ulong                         magic;            // Expert ID (magic mumber)
   ulong                         order;            // Order ticket
//   string                        symbol;           // Trade symbol
   double                        volume;           // Requested volume for a deal in lots
   double                        price;            // Price
   double                        stoplimit;        // StopLimit price of the order
   double                        sl;               // Stop Loss price of the order
   double                        tp;               // Take Profit price of the order
   ulong                         deviation;        // Maximal possible deviation from the requested price
   ENUM_ORDER_TYPE               type;             // Order type
   ENUM_ORDER_TYPE_FILLING       type_filling;     // Order execution type
   ENUM_ORDER_TYPE_TIME          type_time;        // Order execution time
   datetime                      expiration;       // Order expiration time (for the orders of ORDER_TIME_SPECIFIED type)
//   string                        comment;          // Order comment
   char                          symbol[SYMBOL_SIZE];
   char                          comment[COMMENT_SIZE];
  };

 void EncodeTradeRequest(const MqlTradeRequest& lht,MqlTradeRequest_ &rht)
 {
      rht.action = lht.action;
      rht.magic = lht.magic;
      rht.order = lht.order;
      StringToCharArray(lht.symbol,rht.symbol);
      rht.volume = lht.volume;
      rht.price = lht.price;
      rht.stoplimit = lht.stoplimit;
      rht.sl = lht.sl;
      rht.tp = lht.tp;
      rht.deviation = lht.deviation;
      rht.type = lht.type;
      rht.type_filling = lht.type_filling;
      rht.type_time = lht.type_time;
      rht.expiration = lht.expiration;
      StringToCharArray(lht.comment,rht.comment);      
 }
 void DecodeTradeRequest(const MqlTradeRequest_ &lht, MqlTradeRequest& rht)
 {
      rht.action = lht.action;
      rht.magic = lht.magic;
      rht.order = lht.order;
      rht.symbol = CharArrayToString(lht.symbol);
      rht.volume = lht.volume;
      rht.price = lht.price;
      rht.stoplimit = lht.stoplimit;
      rht.sl = lht.sl;
      rht.tp = lht.tp;
      rht.deviation = lht.deviation;
      rht.type = lht.type;
      rht.type_filling = lht.type_filling;
      rht.type_time = lht.type_time;
      rht.expiration = lht.expiration;
      rht.comment = CharArrayToString(lht.comment);
 }
  
 struct TradeRequestBuffer
 {
   //char     buf[344];
   char       buf[sizeof(MqlTradeRequest_)];
 };
 
 struct FollowOrderPacket
 {
   ushort   cmd;
   ushort   param;
   ushort   dataLen;
 };
 struct FollowOrderPacketBuffer
 {
   //char     buf[6];
   char       buf[sizeof(FollowOrderPacket)];
 };
 const int PACKET_HEAD_SIZE = sizeof(FollowOrderPacket);
 struct UdpServerAddrResp
 {
   char      ip[32];
   ushort    port;
 };
 struct UdpServerAddrRespBuffer
 {
   //char     buf[32+2];
   char       buf[sizeof(UdpServerAddrResp)];
 };
 struct PositionInfo
 {
   ENUM_POSITION_TYPE m_type;
   double            m_volume;
   double            m_price_open;
   double            m_price_current;
   double            m_stop_loss;
   double            m_take_profit;
   double            m_profit;
   double            m_commission;
   double            m_swap;
   
   datetime          m_time;
   char              m_symbol[SYMBOL_SIZE];
   char              m_comment[COMMENT_SIZE];
 };
 struct PositionInfoBuffer
 {
   char     buf[sizeof(PositionInfo)];
 };
 struct OrderInfo
 {
   ulong             m_ticket;
   ENUM_ORDER_TYPE   m_type;
   ENUM_ORDER_STATE  m_state;
   ENUM_ORDER_TYPE_FILLING m_filling;
   ENUM_ORDER_TYPE_TIME m_type_time;
   datetime          m_expiration;
   double            m_price_open;
   double            m_volume_curr;
   double            m_stop_loss;
   double            m_take_profit;
   datetime          m_time_setup;
   datetime          m_time_done;
   char              m_symbol[SYMBOL_SIZE];
   char              m_comment[COMMENT_SIZE];
 };
 struct OrderInfoBuffer
 {
   char     buf[sizeof(OrderInfo)];
 };
 struct DealInfo
 {
   ulong             m_ticket;
   ulong             m_order;
   ENUM_DEAL_TYPE    m_deal_type;
   ENUM_DEAL_ENTRY   m_entry;
   double            m_price;
   double            m_volume;
   double            m_commission;
   double            m_swap;
   double            m_profit;
   char              m_symbol[SYMBOL_SIZE];
   char              m_comment[COMMENT_SIZE];
 };
 struct DealInfoBuffer
 {
   char     buf[sizeof(DealInfo)];
 };
 struct AccountInfo
 {
   long              m_login;
   ENUM_ACCOUNT_TRADE_MODE m_trade_mode;
   long              m_leverage;
   ENUM_ACCOUNT_STOPOUT_MODE  m_margin_mode;
   bool              m_trade_allowed;
   bool              m_trade_expert;
   int               m_limit_orders;
   double            m_balance;
   double            m_credit;
   double            m_profit;
   double            m_equity;
   double            m_margin;
   double            m_free_margin;
   double            m_margin_level;
   double            m_margin_call;
   double            m_margin_stop_out;
   
   char              m_name[64];
   char              m_server[64];
   char              m_currency[64];
   char              m_company[64];
   
 };
 struct AccountInfoBuffer
 {
   char     buf[sizeof(AccountInfo)];
 };
 
 #define    CMD_Server_AccountInfo        0x01
 #define    CMD_Server_TradeRequest       0x02
 #define    CMD_Server_PositionInfo       0x03
 #define    CMD_Server_OpenOrder          0x04
 #define    CMD_Server_HistoryOrder       0x05
 #define    CMD_Server_Deal               0x06
 